package com.xingda.packing.dmds.minitor.controller;

import com.xingda.packing.dmds.domain.CacheLocation;
import com.xingda.packing.dmds.domain.ProductBoxLocation;
import com.xingda.packing.dmds.domain.ShuntProductionLine;
import com.xingda.packing.dmds.http.PackingRequireHttp;
import com.xingda.packing.dmds.http.WheelHttp;
import com.xingda.packing.dmds.led.LedOperateService;
import com.xingda.packing.dmds.minitor.entity.PkBoxLocation;
import com.xingda.packing.dmds.minitor.mapper.PkActionMapper;
import com.xingda.packing.dmds.minitor.mapper.PkBoxLocationMapper;
import com.xingda.packing.dmds.task.ticket.AbstractTaskTicket;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.*;

@RestController("debugDMDSController")
@RequestMapping("/api/debug")
@Slf4j
public class DebugController {
    @Resource
    private ShuntProductionLine shuntProductionLine;
    @Resource
    private LedOperateService ledOperateService;
    @Resource
    private PkBoxLocationMapper pkBoxLocationMapper;
    @Resource
    private PkActionMapper pkActionMapper;

    @GetMapping("getProductMap")
    public HashMap<String, Integer> getProductMap(int area) {
        return shuntProductionLine.getPackProductionLine().getProductArea().getProductMap(area);
    }

    @GetMapping("getNoProductMap")
    public HashMap<String, Integer> getNoProductMap(int area) {
        return shuntProductionLine.getPackProductionLine().getProductArea().getNotProductMap(area);
    }

    @GetMapping("getProductList")
    public List<ProductBoxLocation> getProductList(int area) {
        return shuntProductionLine.getPackProductionLine().getProductArea().getLocationList(area);
    }

    @GetMapping("getTaskTicket")
    public AbstractTaskTicket abstractTaskTicket(int area) {
        return shuntProductionLine.getPackProductionLine().getRobot().getTaskTicket(area);
    }

    @GetMapping("getCacheNum")
    public List<String> getCacheNum() {
        List<String> list = new ArrayList<>();
        int maxB80LocationRNo = shuntProductionLine.getPackProductionLine().getCacheArea().getMaxB80LocationRNo();
        int maxB60LocationRNo = shuntProductionLine.getPackProductionLine().getCacheArea().getMaxB60LocationRNo();
        int maxB80LocationLNo = shuntProductionLine.getPackProductionLine().getCacheArea().getMaxB80LocationLNo();
        int maxB60LocationLNo = shuntProductionLine.getPackProductionLine().getCacheArea().getMaxB60LocationLNo();
        list.add("左侧B80最大位置号:" + maxB80LocationLNo);
        list.add("======================================");
        list.add("左侧B40/B60最大位置号:" + maxB60LocationLNo);
        list.add("======================================");
        list.add("右侧B80最大位置号:" + maxB80LocationRNo);
        list.add("======================================");
        list.add("右侧B40/B80最大位置号" + maxB60LocationRNo);
        return list;
    }

    @GetMapping("getWheelInfo")
    public String getWheelInfo(String barCode) {
        return WheelHttp.queryIntermediate(barCode).toJSONString();
    }

    @GetMapping("getTaskInfo")
    public String getTaskInfo(String taskCode) {
        return PackingRequireHttp.searchPackingBoxInspection(taskCode, false, false, false, true).toJSONString();
    }

    @GetMapping("getNextNo")
    public Optional<CacheLocation> nextPutLocationNo(int wheelTypeCode, int area) {
        return shuntProductionLine.getPackProductionLine().getCacheArea().nextPutLocationNo(wheelTypeCode, area);
    }

    @GetMapping("led")
    public void debugLED() {
        List<PkBoxLocation> pkBoxLocations = pkBoxLocationMapper.selectList(null);
        for (PkBoxLocation pkBoxLocation : pkBoxLocations) {
            ledOperateService.write(pkBoxLocation.getBoxLocationName(), pkBoxLocation.getLedIp(), pkBoxLocation.getLedPort());
        }
    }

    @GetMapping("action")
    public List<Map<String, Object>> action() {
        return pkActionMapper.selectTotalWorkDurationByAreaAndDate();
    }
}
